<template>
  <div>
    <r-search
      :searchForm="searchForm"
      :searchHandle="searchHandle"
      :searchData="searchData"
    />
    <r-table
      :tableHandles="tableHandles"
      :isSelection="false"
      :isIndex="false"
      :isPagination="true"
      :tableData="tableData"
      :tableCols="tableCols"
      :tablePage="tablePage"
      :isHandle="true"
      @refresh="init()"
    >
    </r-table>
    <r-form
      :formRules="formRules"
      :formHandle="formHandle"
      :formData="formData"
      :formCols="formCols"
      ref="elForm"
      labelWidth="200px"
      dialogWidth="700px"
      :isHandle="true"
    />
  </div>
</template>
<script>
export default {
  data() {
    return {
      searchData: {
        type: 2 //1 BatteryDeposit  2 车辆Deposit
      },
      searchForm: [
        // {
        //   type: "select",
        //   prop: "type",
        //   placeholder: "TypeName",
        //   width: 150,
        //   options: [
        //     { label: "BatteryDeposit", value: 1 },
        //     { label: "车辆Deposit", value: 2 },
        //   ]
        // },
      ],
      searchHandle: [
        // {
        //   label: "Search",
        //   type: "success",
        //   plain: true,
        //   icon: "el-icon-search",
        //   handle: e => this.init()
        // },
        // {
        //   label: "Reset",
        //   buttonss: true,
        //   icon: "el-icon-refresh-right",
        //   handle: e => {
        //     this.searchData = {
        //       name: "",
        //     };
        //     this.init();
        //   }
        // }
      ],

      tableHandles: [
        {
          label: "Add",
          type: "primary",
          plain: true,
          isShow: true,
          handle: e => this.elFormVisible(),
          isShow: row => {
            return sessionStorage.role == "operator" ? true : false;
          }
        }
      ],
      tableData: [],
      tableCols: [
        {
          label: "Agent Name",
          prop: "name"
        },
        {
          label: "TypeName",
          prop: "btType"
        },
        {
          label: "Deposit Money",
          prop: "money"
        },
        {
          label: "Describe",
          prop: "remarks"
        },
        {
          label: "Operations",
          type: "button",
          width: 120,
          btnList: [
            {
              label: "Edit",
              type: "primary",
              size: "mini",
              plain: true,
              handle: row => this.elFormDetail(row)
            }
          ]
        }
      ],
      tablePage: { limit: 10, page: 1, total: 0 },
      formData: {
        type: 2,
        id: "",
        agentId: "",
        money: "",
        remarks: "",
        btType: "",
        btTypeId: ""
      },
      btTypeIdList: [],
      formCols: [
        {
          type: "select",
          label: "Agent",
          placeholder: "Please Select",
          prop: "agentId",
          width: 350,
          options: []
        },
        {
          type: "select",
          label: "CarType",
          placeholder: "Please Select",
          prop: "btType",
          width: 350,
          options: []
        },
        {
          type: "input",
          label: "Deposit Money",
          placeholder: "Please Enter",
          prop: "money",
          width: 350,
          remark: "元"
        },
        {
          type: "textarea",
          label: "Car Deposit Describe",
          placeholder: "Please Enter",
          prop: "remarks",
          width: 350
        }
      ],
      formRules: {
        id: [{ required: true, message: "Please Enter", trigger: "blur" }],
        agentId: [
          { required: true, message: "Please Select", trigger: "blur" }
        ],
        money: [
          { required: true, message: "Please Enter", trigger: "blur" }
          // { validator: checkToFixedTwo, trigger: 'blur' }
        ],
        remarks: [{ required: true, message: "Please Enter", trigger: "blur" }],
        btType: [{ required: true, message: "Please Enter", trigger: "blur" }]
        // btTypeId: [{ required: true, message: 'Please Enter', trigger: 'blur' }],
      },
      formHandle: [
        {
          label: "Cancel",
          handle: e => this.elFormVisible()
        },
        {
          label: "Confirm",
          type: "success",
          handle: e => this.elFormSubmit()
        }
      ]
    };
  },
  watch: {
    "formData.btType": {
      handler(newValue) {
        if (newValue) {
          this.btTypeIdList.forEach(i => {
            let id = newValue.split(":")[1];
            if (Number(i.id) === Number(id)) {
              this.formData.btTypeId = i.id;
              this.formData.btType = i.name;
            } else {
              return;
            }
          });
        } else {
          this.isformDatas = false;
          return;
        }
      },
      deep: true // 深度监听
    }
  },
  async mounted() {
    this.init();
    this.agent_list();
    this.carTypeFn();
  },

  methods: {
    async init() {
      let parm = {
        ...this.searchData,
        ...{
          pageNum: this.tablePage.page,
          pageSize: this.tablePage.limit
        }
      };
      let res = await this.apis.depositList(parm);
      if (res) {
        this.tableData = res.rows;
        this.tablePage.total = res.total;
      }
    },

    // 拿到车辆Typeid
    async carTypeFn() {
      let parm = {
        pageNum: 1,
        pageSize: 100,
        name: ""
      };
      let res = await this.apis.carTypeList(parm);
      this.btTypeIdList = res.rows || [];
      if (res.rows.length !== 0) {
        let options = [];
        res.rows.forEach(i => {
          options.push({
            value: i.name + ":" + i.id,
            label: i.name
          });
        });

        this.formCols[
          this.labIndex2(this.formCols, "btType")
        ].options = options;
      }
    },

    //Agent列表
    async agent_list() {
      const res = await this.apis.agent_list({
        pageNum: 1,
        pageSize: 100
      });
      var list = res.rows || [];
      let options = [];
      list.map(res => {
        options.push({
          label: res.name,
          value: res.id
        });
      });
      this.formCols[this.labIndex2(this.formCols, "agentId")].options = options;
    },

    // Edit/Add---确定按钮
    async elFormSubmit() {
      this.$refs.elForm.$refs.ruleForm.validate(async valid => {
        if (valid) {
          var res;
          if (this.formData.id) {
            //Edit
            res = await this.apis.carDepositUpdate(this.formData);
          } else {
            //Add
            res = await this.apis.depositSave(this.formData);
          }

          setTimeout(() => {
            this.$message({
              type: res.code == 200 ? "success" : "error",
              message: res.msg
            });
            this.init();
            this.elFormVisible();
          }, 800);
        }
      });
    },

    // Edit按钮
    elFormDetail(row) {
      Object.keys(this.formData).forEach(key => {
        this.formData[key] = row[key];
      });
      this.formData.id = row.id;
      this.elFormVisible();
    }
  }
};
</script>
<style scoped lang="scss"></style>
